# -*- coding: utf-8 -*-
# @Author  : BoPo
# @Time    : 2022/1/4 11:28
# @Function:
import click
from loguru import logger

from .parser import Fetch, parser_info


@click.group()
def cli():
    pass


@cli.command(help='下载最新更新模型')
@click.option('-n', "--num", default=1, help="下载最大分页数")
@click.option('-z', "--zip", type=bool, default=False, help="是否下载压缩包")
def newest(num, zip):
    Fetch(num).batch(uri='newest', file_name="storage/newest.json", zip_flag=zip)


@cli.command(help='下载指定用户模型')
@click.option('-u', "--username", type=str, help="_Downloads the object of a specified user")
@click.option('-n', "--num", default=1, help="It takes the member objects uploaded")
@click.option("--zip", type=bool, help="_Downloads the object of a zip")
def member(username, num, zip):
    Fetch(num).download(uri=f"users/{username}/things", file_name='storage/' + str(username) + ".json", zip_flag=zip)


@cli.command(help='下载关键字搜索模型')
@click.option('-n', "--num", default=1, help="It takes the member objects uploaded")
@click.option("--cid", default=1, help="It takes the member objects uploaded")
@click.option('-k', "--keyword", type=str, help="_Downloads the objects that match the keyword. 12 objects per page\n Example: --search 'star wars'")
@click.option("--zip", type=bool, help="_Downloads the object of a zip")
def search(keyword, num, zip, cid):
    Fetch(num).batch(uri=f"search/{keyword}", file_name=f'storage/{str(keyword)}.json', mode="search", zip_flag=zip)


@cli.command(help='下载 Traffic 模型')
@click.option('-n', "--num", default=1, help="It takes the member objects uploaded")
@click.option('-t', "--traffic", default='traffic light', help="Check new users that have done a traffic light, adding them to a black list")
def traffic(traffic, num, zip):
    for i in range(1, num + 1):
        logger.debug("Page: {}".format(i))
        parser_info(f"search/{traffic}", {'page': i}, "storage/traffic_lights.json")


@cli.command(help='下载专题采集模型')
@click.option('-n', "--num", default=1, help="It takes the member objects uploaded")
@click.option('-s', "--spec", type=str, help="_Downloads the collection with the specified id")
@click.option("--zip", type=bool, help="_Downloads the object of a zip")
def collect(spec, num, zip):
    Fetch(num).batch(uri=f"collections/{spec}/things/", file_name=f'storage/{spec}.json', zip_flag=zip)


@cli.command(help='下载指定things模型')
@click.option('-t', "--tid", type=str, help="_Downloads the thing with the thing id")
def thing(tid):
    Fetch().batch(uri=f"things/{tid}", file_name=f'storage/{tid}.json')


@cli.command(help="It takes the member objects uploaded")
@click.option('-n', "--num", default=1, help="It takes the member objects uploaded")
@click.option('-u', "--username", type=str, help="_Downloads the object of a specified user")
@click.option("--zip", type=bool, help="_Downloads the object of a zip")
def like(username, num, zip):
    Fetch(num).download(uri=f"users/{username}/likes", file_name=f"storage/{username}_likes.json")


if __name__ == "__main__":
    cli()
